package com.ruoyi.web.service.stock.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.domain.stock.Stock;
import com.ruoyi.web.domain.stock.Storage;
import com.ruoyi.web.mapper.stock.StockMapper;
import com.ruoyi.web.mapper.stock.StorageMapper;
import com.ruoyi.web.pojo.dto.stock.StorageRep;
import com.ruoyi.web.service.stock.IStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class StorageService implements IStorageService {

    private final StorageMapper storageMapper;
    private final StockMapper stockMapper;

    public StorageService(StorageMapper storageMapper, StockMapper stockMapper) {
        this.storageMapper = storageMapper;
        this.stockMapper = stockMapper;
    }

    /**
     * 查询入库明细
     *
     * @param id 入库明细主键
     * @return 入库明细
     */
    @Override
    public Storage selectStorageById(Long id)
    {
        Storage storage = storageMapper.selectStorageById(id);
        if (SecurityUtils.isParkManager()){
            return storage;
        }else if (SecurityUtils.getUserId().equals(storage.getUserId())){
            return storage;
        } else {
            return null;
        }
    }

    /**
     * 查询入库明细列表
     *
     * @param storage 入库明细
     * @return 入库明细
     */
    @Override
    public List<StorageRep> selectStorageList(Storage storage)
    {
        if (SecurityUtils.isParkManager()){
            storage.setUserId(null);
        } else{
            storage.setUserId(SecurityUtils.getUserId());
        }
        if (Objects.nonNull(storage.getStartTime()) && Objects.nonNull(storage.getEndTime())){
            storage.setStartTime(DateUtils.minDate(storage.getStartTime()));
            storage.setEndTime(DateUtils.maxDate(storage.getEndTime()));
        }
        return storageMapper.selectStorageList(storage);
    }

    /**
     * 新增入库明细
     *
     * @param storage 入库明细
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertStorage(Storage storage)
    {
        storage.setCreateBy(SecurityUtils.getUsername());
        storage.setUpdateTime(DateUtils.getNowDate());
        storage.setUserId(SecurityUtils.getUserId());
        //修改库存
        Stock stock = updateStock(storage);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String s = objectMapper.writeValueAsString(stock);
            storage.setRemark(s);
        } catch (Exception e) {
            throw new ServiceException("库存转换异常");
        }
        storage.setUnit(stock.getUnit());
        storage.setProduct(stock.getProduct());
        storage.setType(stock.getType());
        return storageMapper.insertStorage(storage);
    }

    private Stock updateStock(Storage storage) {
        Stock stock = stockMapper.selectStockById(storage.getStockId());
        if (Objects.isNull(stock)){
            throw new ServiceException("未查询到库存");
        }
        stock.setNumber(stock.getNumber() + storage.getNumber());
        stock.setUpdateBy(SecurityUtils.getUsername());
        stock.setUpdateTime(DateUtils.getNowDate());
        stockMapper.updateStock(stock);
        return stock;
    }

    /**
     * 修改入库明细
     *
     * @param storage 入库明细
     * @return 结果
     */
    @Override
    public int updateStorage(Storage storage)
    {
        storage.setUpdateTime(DateUtils.getNowDate());
        return storageMapper.updateStorage(storage);
    }

    /**
     * 批量删除入库明细
     *
     * @param ids 需要删除的入库明细主键
     * @return 结果
     */
    @Override
    public int deleteStorageByIds(List<Long> ids)
    {
        return storageMapper.deleteStorageByIds(ids);
    }

    /**
     * 删除入库明细信息
     *
     * @param id 入库明细主键
     * @return 结果
     */
    @Override
    public int deleteStorageById(Long id)
    {
        return storageMapper.deleteStorageById(id);
    }
}
